#include "head.h"
// 切割加上检查用户输入命令的函数
// 参数1，客户输入的字符串
// 参数2，协议结构体指针
int check_command(char *command, transport_protocol_t *train){
    int i = 0;

    while(1){
        if(command[i] == '\0'){
            break;
        }
        if(command[i] == '\\'){
            printf("有非法字符\n");
            train->flag_stat = -1;
            return -1;
        }
        i++;
    }
    i = 0;
    while(1){
        if(command[i] == '\0'){
            train->flag_stat = -1;
            return -1;
        }
        if(command[i] == ' '){
            i++;
        }else{
            break;
        }
    }

    char buf[60] = {0};
    strcpy(buf, command);
    char *p = strtok(buf," "); 
    train->flag_work = command_judge(p);
    switch(train->flag_work){
    case 1: // 调用ls函数
        if(check_format(p, train)){
            if(check_add(p)){
                // TODO:ls 
                break;
            }else{
                train->flag_stat=-1;
                break;
            }
        }else{
            break;
        }

    case 2: // cd
        if(check_format(p, train)){
            if(check_add(p)){
                // TODO:cd  
                break;
            }else{
                train->flag_stat=-1;
                break;
            }
        }else{
            train->flag_stat=-1;
            break;
        }
    case 3: // rm
        if(check_format(p, train)){
            if(check_add(p)){
                // TODO: rm  
                break;
            }else{
                train->flag_stat=-1;
                break;
            }
        }else{
            train->flag_stat=-1;
            break;
        }
    case 4: // pwd 
        if(check_add(p)){
            // TODO: pwd
            break;
        }else{
            train->flag_stat=-1;
            break;
        }
    case 5: // puts
        if(check_format(p, train)){
            if(check_add(p)){
                // TODO: puts
                break;
            }else{
                train->flag_stat=-1;
                break;
            }
        }else{
            train->flag_stat=-1;
            break;
        }
    case 6:
        if(check_format(p, train)){        
            if(check_add(p)){
                // TODO: gets
                break;
            }else{
                train->flag_stat=-1;
                break;
            }
        }else{
            train->flag_stat=-1;
            break;
        }

    case 7:
        if(check_format(p, train)){
            if(check_add(p)){
                // TODO:mkdir
                break;
            }else{
                train->flag_stat=-1;
                break;
            }
        }else{
            train->flag_stat=-1;
            break;
        }
    case 8: // rmdir
        if(check_format(p, train)){
            if(check_add(p)){
                break;
            }else{
                train->flag_stat=-1;
                break;
            }
        }else{
            train->flag_stat=-1;
            break;
        }
    case -1:
        train->flag_stat=-1;
        break;
    default:
        break;

    }
    return 0;
}

// 对命令第一个单词进行检查
// 参数1，切割好的字符串
int command_judge(char *p){
    if(strcmp(p,"ls") == 0){
        return 1;
    }
    if(strcmp(p,"cd") == 0){
        return 2;
    }
    if((strcmp(p,"remove") == 0) || (strcmp(p,"rm") == 0)){
        return 3;
    }
    if(strcmp(p,"pwd") == 0){
        return 4;
    }
    if(strcmp(p,"puts") == 0){
        return 5;
    }
    if(strcmp(p,"gets") == 0){
        return 6;
    }
    if(strcmp(p,"mkdir") == 0){
        return 7;
    }                            
    if(strcmp(p,"rmdir") == 0){
        return 8;
    }
    if(strcmp(p,"help")==0){
        return 9;
    }
    // 函数执行到此处说明没有对应的字符串匹配
    return -1;

}

// 如果后续需要有字符使用该函数检查
// 参数1，切割好的字符串
// 参数2，协议结构体指针
bool check_format(char *p, transport_protocol_t *test){
    p = strtok(NULL," ");
    if(p == NULL){
        return false;
    }
    strcpy(test->buf, p); // 运行到此处说明格式正确
    test->command_len = strlen(test->buf);
    // printf("%s\n", test->buf);
    return true;
}
//  如果后续不应该有字符使用该函数检查 
//  参数1 切割好的字符串
bool check_add(char *p){
    p = strtok(NULL," ");
    if(p != NULL){
        return false;
    }
    return true;
}

